/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.udistrital.conce.presentacion.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author timotheos
 */
public class DateOnlyConverter implements Converter {

    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        return value;
    }

    /**
     * Strips of the time part from a full date string and formats it
     * according to the Falabella standards e.g 19112009143023 ==> 19/11/2009
     * @param arg0
     * @param arg1
     * @param value
     * @return
     */
    public String getAsString(FacesContext arg0, UIComponent arg1, Object value) {
        String date = "";
        if ( value==null ){
            return date;
        }
        if ((date = (String) value).length() > 13) {
            if (!StringUtils.isEmpty(date)) {
                // Strip off the time part
                date = date.substring(0, 8);

                // Format the date acordingly
                date = date.substring(0, 2) + "/" + date.substring(2, 4) + "/" + date.substring(4);
            }
        } else {
            date = date.replace("-", "/");
        }

        return date;
    }
}